#include "nr_device_group.h"

nr_device_group::nr_device_group()
{
}

nr_device_group::nr_device_group(nr_device_group& group)
{
    copy_data(group);
}

nr_device_group::~nr_device_group()
{
}

nr_device_group& nr_device_group::operator = (nr_device_group &group)
{
    // Copy it 
    copy_data(group);

    return *this;
}

void nr_device_group::copy_data(nr_device_group& group)
{
    this->name() = group.name();
    
    // Copy sub-groups
    for(nr_device_group_hash::iterator it = group.groups().begin();
        it != group.groups().end(); ++it)
    {
        nr_device_group *new_group = new nr_device_group(*it.value());
        this->groups().insert(it.key(), new_group);
    }
    
    // Copy sub-items
    for(nr_device_items::iterator it = group.items().begin();
        it != group.items().end(); ++it)
    {
        nr_device_item *new_item = new nr_device_item(*it.value());
        this->items().insert(it.key(), new_item);
    }

}

nr_device_item* nr_device_group::find_device_item_by_id(const nr_device_id_t &device_id)
{
    // Find in each sub-group
    foreach(nr_device_group* group, this->groups())
    {
        nr_device_item* item = group->find_device_item_by_id(device_id);
        if (item != NULL)
            return item;
    }
    
    // Find in each sub-item
    foreach(nr_device_item* sub_item, this->items())
    {
        nr_device_item* item = sub_item->find_device_item_by_id(device_id);
        if (item != NULL)
            return item;
    }
    
    // Not found
    return NULL;
}

void nr_device_group::serialize(QDataStream& data_stream)
{
    // Save each field
    data_stream << this->name();
    
    // Save the groups
    data_stream << this->groups().count();
    for(nr_device_groups::iterator it = this->groups().begin();
        it != this->groups().end(); ++it)
    {
        data_stream << it.key();
        nr_device_group* child_group = it.value();
        child_group->serialize(data_stream);
    }
    
    // Save the sub-items
    data_stream << this->items().count();
    for(nr_device_items::iterator it = this->items().begin();
        it != this->items().end(); ++it)
    {
        data_stream << it.key();
        nr_device_item* child_item = it.value();
        child_item->serialize(data_stream);
    }

    nr_object::serialize(data_stream);
}

void nr_device_group::deserialize(QDataStream& data_stream)
{
    // Get each field
    data_stream >> this->name();
    
    // Get the groups
    int child_count = 0;
    data_stream >> child_count;
    for(int i = 0; i < child_count; ++i)
    {
        nr_device_id_t id;
        nr_device_group* group = new nr_device_group;

        data_stream >> id;
        group->deserialize(data_stream);
        this->groups().insert(id, group);
    }

    // Get the sub-items
    child_count = 0;
    data_stream >> child_count;
    for(int i = 0; i < child_count; ++i)
    {
        nr_device_id_t id;
        nr_device_item* item = new nr_device_item;

        data_stream >> id;
        item->deserialize(data_stream);
        this->items().insert(id, item);
    }

    nr_object::deserialize(data_stream);
}